package com.hxwm.hwdbtool.entity.dbmigration;

import com.hxwm.hwdbtool.util.HwUtil;

import java.util.ArrayList;
import java.util.List;

public class MapperSheet {

    private MapperTable newTable;
    private MapperTable oldTable;
    private List<MapperTable> oldTableList;
    private List<MapperTableRelation> tableRelationList = new ArrayList<>();
    private String tableJoinType;
    private String whereCondition;
    private Integer sheetLineNum = 0;
    private Integer sheetColNum = 0;
    private boolean isMultiTable = false;
    private String oldTableFieldSql = "";
    private String newTableFieldSql = "";

    public MapperSheet(MapperTable newTable, MapperTable oldTable) {
        this.newTable = newTable;
        this.oldTable = oldTable;
    }

    /**
     * 返回插入新表的表字段字符串
     * 非全表字段，只包含用户指定的需要赋值的字段
     * @param newTable
     * @param oldTableList
     * @param lineNum
     */
    public MapperSheet(MapperTable newTable, List<MapperTable> oldTableList,Integer lineNum) {
        this.newTable = newTable;
        this.oldTableList = oldTableList;
        this.sheetLineNum = lineNum;
        for (int i = 0; i < lineNum; i++) {
            MapperField newField = newTable.getTableFields().get(i);
            int tableIndex = 0;
            for (MapperTable mapperTable : oldTableList) {
                MapperField oldField = mapperTable.getTableFields().get(i);
                if(!HwUtil.isNullOrEmpty(newField.getFieldName()) && !HwUtil.isNullOrEmpty(oldField.getFieldName())){
                    //新酒标字段一一对应
                    tableRelationList.add(new MapperTableRelation(oldField,newField,tableIndex+""));
                    oldTableFieldSql = oldTableFieldSql + mapperTable.getTableName() + "." + oldField.getFieldName() + ",";
                    newTableFieldSql = newTableFieldSql + newField.getFieldName() + ",";
                    break;
                }else if(!HwUtil.isNullOrEmpty(newField.getFieldName()) && !HwUtil.isNullOrEmpty(newField.getSpecialCondition())){
                    //新表字段非空且特殊域非空
                    tableRelationList.add(new MapperTableRelation(oldField,newField,tableIndex+""));
                    newTableFieldSql = newTableFieldSql + newField.getFieldName() + ",";
                    break;
                }else if(!HwUtil.isNullOrEmpty(newField.getFieldName()) && !HwUtil.isNullOrEmpty(newField.getRelRelation())){
                    //新表字段非空且关联关系字段非空
                    tableRelationList.add(new MapperTableRelation(oldField,newField,tableIndex+""));
                    newTableFieldSql = newTableFieldSql + newField.getFieldName() + ",";
                    break;
                }else if(!HwUtil.isNullOrEmpty(oldField.getFieldName()) && HwUtil.isNullOrEmpty(newField.getFieldName())){
                    tableRelationList.add(new MapperTableRelation(oldField,newField,tableIndex+""));
                    oldTableFieldSql = oldTableFieldSql + mapperTable.getTableName() + "." + oldField.getFieldName() + ",";
                    break;
                }
                tableIndex ++;
            }
        }
        oldTableFieldSql = oldTableFieldSql.substring(0,oldTableFieldSql.lastIndexOf(","));
        newTableFieldSql = newTableFieldSql.substring(0,newTableFieldSql.lastIndexOf(","));
    }

    public Integer getSheetLineNum() {
        return sheetLineNum;
    }

    public void setSheetLineNum(Integer sheetLineNum) {
        this.sheetLineNum = sheetLineNum;
    }

    public MapperTable getNewTable() {
        return newTable;
    }

    public void setNewTable(MapperTable newTable) {
        this.newTable = newTable;
    }

    public MapperTable getOldTable() {
        return oldTable;
    }

    public void setOldTable(MapperTable oldTable) {
        this.oldTable = oldTable;
    }

    public List<MapperTable> getOldTableList() {
        return oldTableList;
    }

    public void setOldTableList(List<MapperTable> oldTableList) {
        this.oldTableList = oldTableList;
    }

    public List<MapperTableRelation> getTableRelationList() {
        return tableRelationList;
    }

    public void setTableRelationList(List<MapperTableRelation> tableRelationList) {
        this.tableRelationList = tableRelationList;
    }

    public String getWhereCondition() {
        return whereCondition;
    }

    public void setWhereCondition(String whereCondition) {
        this.whereCondition = whereCondition;
    }

    public boolean isMultiTable() {
        return isMultiTable;
    }

    public void setMultiTable(boolean multiTable) {
        isMultiTable = multiTable;
    }

    public String getOldTableFieldSql() {
        return oldTableFieldSql;
    }

    public void setOldTableFieldSql(String oldTableFieldSql) {
        this.oldTableFieldSql = oldTableFieldSql;
    }

    public String getNewTableFieldSql() {
        return newTableFieldSql;
    }

    public void setNewTableFieldSql(String newTableFieldSql) {
        this.newTableFieldSql = newTableFieldSql;
    }

    public Integer getSheetColNum() {
        return sheetColNum;
    }

    public void setSheetColNum(Integer sheetColNum) {
        this.sheetColNum = sheetColNum;
    }

    public String getTableJoinType() {
        return tableJoinType;
    }

    public void setTableJoinType(String tableJoinType) {
        this.tableJoinType = tableJoinType;
    }
}
